//
//  CarViewModel.m
//  易车界面
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CarViewModel.h"

@interface CarViewModel ()

@property (nonatomic, strong) NSMutableArray *headDataArr, *bodyDataArr;
@property (nonatomic, strong) NSDictionary *dataDic;
- (void)getData:(dataBlock)dataBlock;

@end

@implementation CarViewModel

- (void)getData:(dataBlock)dataBlock
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"YiChe" ofType:@"plist"];
    _dataDic = [NSDictionary dictionaryWithContentsOfFile:path];
    dataBlock(self.headDataArr, self.bodyDataArr);
}

- (NSMutableArray *)headDataArr
{
    if (!_headDataArr) {
        _headDataArr = [NSMutableArray array];
        _headDataArr = [self analysisArr:_dataDic[@"data"][@"cycleImage"] andModelClass:@"CarHeadModel"].mutableCopy;
    }
    return _headDataArr;
}

- (NSMutableArray *)bodyDataArr
{
    if (!_bodyDataArr) {
        _bodyDataArr = [NSMutableArray array];
        _bodyDataArr = [self analysisArr:_dataDic[@"data"][@"list"] andModelClass:@"CarModel"].mutableCopy;
    }
    return _bodyDataArr;
}

- (NSArray *)analysisArr:(NSArray *)arr andModelClass:(NSString *)modelClass
{
    NSMutableArray *mArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        id model = [[NSClassFromString(modelClass) alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [mArr addObject:model];
    }
    
    return mArr;
}


@end
